package com.atguigu.lease.web.admin.schedule;

import com.atguigu.lease.model.entity.LeaseAgreement;
import com.atguigu.lease.web.admin.service.LeaseAgreementService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

import static com.atguigu.lease.model.enums.LeaseStatus.*;

/**
 * Package: com.atguigu.lease.web.admin.schedule
 *
 * @Description:
 * @Author xiongkunYang
 * @Create 2025/5/3 22:01
 */
@Component
@RequiredArgsConstructor
public class ScheduledTasks {

    private final LeaseAgreementService leaseAgreementService;

    // 每天午夜执行检查租赁协议状态的任务
    @Scheduled(cron = "0 0 0 * * ?")
    public void checkLeaseAgreementStatus() {
        // 更新租赁协议状态为过期，如果当前日期超过租赁结束日期
        // 仅当协议状态为已签约、退租待确认或续租待确认时进行更新
        this.leaseAgreementService.update(
                new LambdaUpdateWrapper<LeaseAgreement>()
                        .le(LeaseAgreement::getLeaseEndDate, new Date())
                        .in(LeaseAgreement::getStatus, SIGNED, WITHDRAWING, RENEWING)
                        .set(LeaseAgreement::getStatus, EXPIRED)
        );
    }

}